<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div th:replace="header::common"></div>
</head>
<body>
    <form id="formId" class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">博客标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">博客分类</label>
            <div class="layui-input-inline">
                <select name="typeId">
                    <option value="1">Java</option>
                    <option value="2">UI</option>
                    <option value="3">H5</option>
                    <option value="4">Python</option>
                    <option value="5">C</option>
                    <option value="6">大数据</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">博客封面</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img width="100px" height="100px" class="layui-upload-img" id="demo1">
                        <input type="hidden" name="image" id="imageId">
                        <p id="demoText"></p>
                    </div>
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">博客内容</label>
            <div class="layui-input-block">
                <textarea name="content" id="contentId" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <button type="button" onclick="submitForm()" class="layui-btn" lay-submit="" lay-filter="demo2">添加</button>
        </div>
    </form>

    <!--
    <form>
        用户名：<input type="text" name="name"/><br/>
        密码：<input type="text" name="password"/><br/>
        邮箱：<input type="text" name="email"/><br/>
        <input type="submit" value="添加"/>
    </form>-->

    <script type="text/javascript" th:src="@{/static/kindeditor/kindeditor.js}"></script>
    <script>
        KindEditor.ready(function() {
            var kindEditorParams = {
                afterBlur: function() {
                    this.sync();
                }
            };

            KindEditor.create('#contentId', kindEditorParams);
        });

        layui.use(['upload'], function() {
            var upload = layui.upload;

            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: '/upload/uploadImage' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(result){
                    if (result.ok) {
                        $('#imageId').val(result.data);
                        $('#demoText').html(''); //置空上传失败的状态
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

        });

        function submitForm() {
            $.post(
                '/blog/add',
                $('#formId').serialize(),   //{'name':'zhansgan','password':'123','email':'123'}
                function(result) {
                    console.log(result);
                    //setInterval(function(){}, 2000);
                    if (result.ok) {
                        mylayer.okMsg(result.msg);
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>